package com.mxprojects.gui
{
	import com.mxprojects.constructors.Dropdown;
	import com.mxprojects.io.CustomEvent;
	import com.mxprojects.io.DeepLinking;
	import com.mxprojects.io.ServerHandler;
	import com.mxprojects.language.Word;
	import com.mxprojects.views.Forums;
	
	import flash.display.MovieClip;
	
	public class ForumDropdown extends MovieClip
	{
		/************************************************
		 * This class is meant to be built and re-used
		 * if someone logs out then it should be cleared
		 * to be built again
		 ************************************************/
		
		private static const FORUM_NAME_PADDING:String = "    ";
		
		private static var dropdown:Dropdown;
		private static var elements:Array = new Array();
		private static var values:Array = new Array();
		private static var forumData:Object;
		private static var serverHandler:ServerHandler = new ServerHandler();
		private static var selectedForumID:int;
		 
		public function ForumDropdown(forumID:int = 0){ 
			//destroy everything you touched
			clearAll();
			
			//set the var for the dropdown to grab
			selectedForumID = forumID;
			
			//see if we have data to work with, if not then we better go get some mmhmmm
			if(Forums.forumData != null){
				forumData = Forums.forumData;
				build();
			}else{
				//guess we need to load them forums
				Forums.load(true, build);
			}
		}
		
		private function build(event:CustomEvent = null):void {
			//get the category/forum data and build the dropdown
			if(event != null){
				forumData = event.getServerData();
				Forums.forumData = forumData;
			}
			
			//create the dropdown
			if(dropdown == null){
				//if we are in another forum...
				var defaultValue:String = "";
				if(String(selectedForumID) != Dropdown.NULL_VALUE){
					defaultValue = String(selectedForumID);
				}
				dropdown = new Dropdown(defaultValue);
				dropdown.addEventListener(Dropdown.SELECT, select, false, 0, true);
				addChild(dropdown);
			}
			
			//loop through the categories and forums
			for(var i:int = 0; i < forumData["categoryCount"]; i++){
				
				//add it to the dropdown
				dropdown.addItem(forumData["category" + i + "title"], Dropdown.NULL_VALUE, false);
				
				//any forums belong to these titles?
				for(var j:int = 0; j < forumData["forumCount"]; j++){
					if(forumData["category" + i + "ID"] == forumData["forum" + j + "categoryID"]){
						dropdown.addItem(FORUM_NAME_PADDING + forumData["forum" + j + "title"], forumData["forum" + j + "forumID"], false);
					}
				}
			}
			
			//update the dropdown with all of the elements
			dropdown.update();
		}
		
		/*
		private function addElement(name:String, value:String, isUnselectable:Boolean = false):void {
			//add the data to the arrays, making sure they aren't already there!
			var elementName:String = "<span class=\"dropdownElement\">" + name + "</span>";
			if(isUnselectable){
				elementName = "<span class=\"dropdownUnselectable\">" + name + "</span>";
			}
			
			if(elements.indexOf(elementName) == -1){
				//good to go, add em in
				elements.push(elementName);
				values.push(value);
			}
		}
		
		private function createDropdown():void {
			//make sure it doesn't exist
			if(dropdown == null){
				
			}
		}
		*/
		
		private function select(event:CustomEvent):void {
			//this fires when a dropdown element is selected
			var value:String = event.customParameter[1];
			if(value != Dropdown.NULL_VALUE){
				//jump to the other forum
				DeepLinking.setLink(Word.THREADS, DeepLinking.createLinkArray(int(value)));
			}
		}
		
		private function clearAll():void {
			//empty the arrays and delete stuff
			elements = new Array();
			values = new Array();
			dropdown = null;
		}

	}
}